#include "include/parammodbus.h"
#include <QDebug>
#include <QFileInfo>
#include <QSettings>
#include <QTextCodec>
ParamModbus::ParamModbus( QObject* parent ) : QObject( parent ) {}

ParamModbus::~ParamModbus() {}

int ParamModbus::ModbusType;

QString ParamModbus::TcpIP;
int     ParamModbus::TcpPort;

QString ParamModbus::SerialPortNameParameter;  //串口名
int     ParamModbus::SerialParityParameter;    //奇偶校验
int     ParamModbus::SerialBaudRateParameter;  //波特率
int     ParamModbus::SerialDataBitsParameter;  //串行数据位
int     ParamModbus::SerialStopBitsParameter;  //停止位
int     ParamModbus::SerialTimeout;            //超时
int     ParamModbus::SerialFrequency;          //发报文间隔
/*****modbus 串口配置*****/
int ParamModbus::StatID;               //站id
int ParamModbus::ModbsuReadFrequency;  //读取数据的频率
int ParamModbus::WriterMaxSize;        //写缓存最大数量，大于丢弃
int ParamModbus::ModbusHighFrequency;  //高频率写的频率

void ParamModbus::readModbusParam() {
    QSettings modbus_param( "./config/Modbus.ini", QSettings::IniFormat );
    modbus_param.setIniCodec( QTextCodec::codecForName( "UTF-8" ) );
    modbus_param.beginGroup( "modbus_init" );
    ModbusType              = modbus_param.value( "ModbusType", 1 ).toInt();
    SerialPortNameParameter = modbus_param.value( "SerialPortNameParameter", "comtys1" ).toString();
    SerialParityParameter   = modbus_param.value( "SerialParityParameter", 0 ).toInt();
    SerialBaudRateParameter = modbus_param.value( "SerialBaudRateParameter", 9600 ).toInt();
    SerialDataBitsParameter = modbus_param.value( "SerialDataBitsParameter", 8 ).toInt();
    SerialStopBitsParameter = modbus_param.value( "SerialStopBitsParameter", 1 ).toInt();
    SerialTimeout           = modbus_param.value( "SerialTimeout", 3000 ).toInt();
    TcpIP                   = modbus_param.value( "TcpIP", "192.168.1.1" ).toString();
    TcpPort                 = modbus_param.value( "TcpPort", 502 ).toInt();
    StatID                  = modbus_param.value( "StatID" ).toInt();
    ModbsuReadFrequency     = modbus_param.value( "ModbsuReadFrequency", 300 ).toInt();
    WriterMaxSize           = modbus_param.value( "WriterMaxSize", 10 ).toInt();
    SerialFrequency         = modbus_param.value( "SerialFrequency", 50 ).toInt();
    modbus_param.endGroup();
}

void ParamModbus::printModbusParam() {
    qDebug() << "SerialPortNameParameter: " << SerialPortNameParameter;
    qDebug() << "SerialParityParameter: " << SerialParityParameter;
    qDebug() << "SerialBaudRateParameter: " << SerialBaudRateParameter;
    qDebug() << "SerialDataBitsParameter: " << SerialDataBitsParameter;
    qDebug() << "SerialStopBitsParameter: " << SerialStopBitsParameter;
    qDebug() << "SerialTimeout: " << SerialTimeout;
    qDebug() << "StatID: " << StatID;
    qDebug() << "ModbusType" << ModbusType;
    qDebug() << "TcpIP" << TcpIP;
    qDebug() << "TcpPort" << TcpPort;
}
